package com.wanxi.servlet.news;

import com.alibaba.fastjson.JSONObject;
import com.wanxi.dto.ResultDto;
import com.wanxi.model.NewsModel;
import com.wanxi.service.Impl.NewsServiceImpl;
import com.wanxi.service.NewsService;
import com.wanxi.util.Tool;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @author Li
 * @Date 2023/5/7 11:59
 */
@WebServlet("/back/FindAllNews")
public class FindAllNewsServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //乱码处理
        req.setCharacterEncoding("UTF-8");
        resp.setCharacterEncoding("UTF-8");
        //接收前端数据
        String title = req.getParameter("title");
        String img = req.getParameter("img");
        String content = req.getParameter("content");
        String time = req.getParameter("time");
        String startTime = req.getParameter("startTime");
        String endTime = req.getParameter("endTime");
        String enable = req.getParameter("enable");
        String newsId = req.getParameter("newsId");
        String remark = req.getParameter("remark");
        String page = req.getParameter("page");
        String limit = req.getParameter("limit");
        //封装model
        NewsModel newsModel = NewsModel.builder()
                .title(Tool.nullToStr(title))
                .content(Tool.nullToStr(content))
                .img(Tool.nullToStr(img))
//                .time(Tool.nullToDate(time))
//                .startTime(Tool.nullToDate(startTime))
//                .endTime(Tool.nullToDate(endTime))
                .time(Tool.nullToStr(time))
                .startTime(Tool.nullToStr(startTime))
                .endTime(Tool.nullToStr(endTime))
                .remark(Tool.nullToStr(remark))
                .enable(Tool.nullToInt(enable))
                .newsId(Tool.nullToInt(newsId))
                .page(Tool.nullToPage(page))
                .limit(Tool.nullToLimit(limit))
                .build();
        //调用service层
        NewsService newsService = new NewsServiceImpl();
        ResultDto resultDto = newsService.findAll(newsModel);
        //将数据返回给前端
        resp.getWriter().println(JSONObject.toJSONString(resultDto));
    }
}
